<?php
/**
 * Created by PhpStorm.
 * User: soft
 * Date: 2016/9/7
 * Time: 17:59
 * extends base_task_abstract implements base_interface_task
 */

//define('ROOT_DIR',realpath(dirname(__FILE__)));
define('ROOT_DIR', realpath(dirname(__FILE__).'/../../../../'));
date_default_timezone_set(
    defined('DEFAULT_TIMEZONE') ? ('Etc/GMT'.(DEFAULT_TIMEZONE>=0?(DEFAULT_TIMEZONE*-1):'+'.(DEFAULT_TIMEZONE*-1))):'UTC'
);

require(ROOT_DIR.'/app/base/kernel.php');
require(ROOT_DIR.'/config/config.php');
kernel::register_autoload();

class b2c_tasks_moveimage {

    public function dorun($m,$r){
        $sql_image = ' select image_id  from sdb_image_image_attach
        where target_type = \'goods\' and
        target_id not in(
            select g.goods_id from sdb_b2c_goods as g LEFT JOIN  sdb_image_image_attach as i on (g.goods_id = i.target_id) group by g.goods_id
            ) ';
        $image_id = $m->select($sql_image);
        foreach($image_id as $_id){
            if($_id) {
                $image_ids[] = '\'' . $_id['image_id'] . '\'';
            }
        }
        if($r->EXISTS('image_ids')){
            $r->del('image_ids');
        }
        $r->SET('image_ids',implode(',',$image_ids));
        echo $r->GET('image_ids');
        echo '================+++++0001+++++++================';
        $sql = 'select url from sdb_image_image where image_id in(
            '.$r->GET('image_ids').'
            )';
        $image_url = $m->select($sql);
        foreach($image_url as $_gid){
            if($_gid) {
                $image_urls[] = $_gid['url'];
            }
        }
        foreach($image_urls as $_url){
            if($_url) {
                $url = explode('/', $_url);
                array_pop($url);
                $url = '/backup/'.implode('/', $url);
                $urls[] = 'mkdir -p '.$url;
//                if(!is_dir($url)){
//                    if( !mkdir($url,0,true) ){
//                        die('Failed to create folders...');
//                    }
//                }
                $cpurl[] = 'mv /data/www/img.qifa.ru/' . $_url . ' /backup/' . $_url;
            }
        }
        if($r->EXISTS('mkdir_urls')){
            $r->del('mkdir_urls');
        }
        $r->SET('mkdir_urls',implode(';',$urls));
        echo $r->GET('mkdir_urls');
        echo '================+++++0002+++++++================';
        if($r->EXISTS('image_urls')){
            $r->del('image_urls');
        }
        $r->SET('image_urls',implode(';',$cpurl));
        echo $r->GET('image_urls');
        echo '================+++++0003+++++++================';
    }

//    public function doimg($m,$r){
//        $sql_image = 'select url from sdb_image_image_attach where image_id in(
//            select image_id from sdb_image_image_attach where target_type=\'goods\'
//            ) and image_id not in('.$r->GET('image_ids').')';
//        $image_url = $m->select($sql_image);
//        echo count($image_url);
//    }

    public function doshell($r){
        $filename = ROOT_DIR.'/dirimage.sh';
        fopen($filename, "w") or die("Unable to open file!");
        $somecontent = $r->GET('mkdir_urls');

        if (is_writable($filename)) {
            if (!$handle = fopen($filename, 'a')) {
                echo "Unable to open file $filename";
                exit;
            }

            if (fwrite($handle, $somecontent) === FALSE) {
                echo "Unable to write file $filename";
                exit;
            }

            echo "success $somecontentto write file $filename";

            fclose($handle);

        } else {
            echo "$filename Unable to write file";
        }
    }

    public function doshell2($r){
        $filename = ROOT_DIR.'/mvimage.sh';
        fopen($filename, "w") or die("Unable to open file!");
        $somecontent = $r->GET('image_urls');

        if (is_writable($filename)) {
            if (!$handle = fopen($filename, 'a')) {
                echo "Unable to open file $filename";
                exit;
            }

            if (fwrite($handle, $somecontent) === FALSE) {
                echo "Unable to write file $filename";
                exit;
            }

            echo "success $somecontentto write file $filename";

            fclose($handle);

        } else {
            echo "$filename Unable to write file";
        }
    }

    public function exec($params=null){
        //在这里写处理代码
        set_time_limit(0);
        $m =  kernel::database();
        $r =  new Redis();
        $r->connect('123.206.32.88','6379');
        $r->auth('22');
        $r->select(10);
        $this->dorun($m,$r);
        $this->doshell($r);
        $this->doshell2($r);
//        $this->doimg($m,$r);
    }
}
$obj = new b2c_tasks_moveimage();
$obj->exec();